﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_Basics.TMS_Oilcost
{
    public class TMS_Oilcosts : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_Oilcosts
    {
        public TMS_Oilcosts(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        #region 油费显示
        public async Task<PageResponse<TMS_OilcostDto>> GetOil(string Oilnumber, string Oilpeop, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_OilcostDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_OilcostModel where 1=1 ";
            if (!string.IsNullOrEmpty(Oilnumber))
            {
                sql += $" and Oilnumber like '%{Oilnumber}%'";
            }
            if (!string.IsNullOrEmpty(Oilpeop))
            {
                sql += $" and Oilpeop  like '%{Oilpeop}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_OilcostDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_OilcostDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 油费添加
        public async Task<int> OilAdds(TMS_OilcostDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_OilcostModel values ('{info.Id}','{info.Oilnumber}','{info.Oilyuan}','{info.Oilml}','{info.Oilkm}','{info.Oilpeop}','{info.Oiltxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 油费删除
        public async Task<int> DeletOil(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_OilcostModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion


    }
}
